﻿package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import android.content.Context;
import android.text.TextUtils;

import com.shuqi.common.Base64;
import com.shuqi.common.ROT13;
import com.shuqi.common.utils.Log4an;
import com.shuqi.common.utils.Util;
import com.shuqi.model.parse.HandlerBase;

/**
 * cdn解析章节内容
 * 
 * @author zhangpeng
 * 
 */
public class ChapterContentCdnParser extends HandlerBase {
    private String TAG = "ChapterContentCdnParser";
    private String chapterContent = null;
    private List<String> chapters = null;
    private int inWhich = 0;
    private static final int CHAPTERCONTENT = 1;

    public ChapterContentCdnParser(Context context) {}

    @Override
    public void startDocument() {
        try {
            super.startDocument();
            chapters = new ArrayList<String>();
        } catch (SAXException e) {
            Log4an.e(TAG, "解析格式错误");
        }
        // bookcontect.formatError();
    }

    public void startElement(String uri, String localName, String qName, Attributes atts) {
        try {
            super.startDocument();
            if (localName.equals("ChapterContent")) {
                this.inWhich = CHAPTERCONTENT;
            } else {
                this.inWhich = 0;
            }
        } catch (SAXException e) {
            Log4an.e(TAG, "解析格式错误");
        }
    }

    public void characters(char[] ch, int start, int length) {
        try {
            super.characters(ch, start, length);
            String value = new String(ch, start, length);
            if (value != null && !value.trim().equals(""))
                switch (inWhich) {
                    case CHAPTERCONTENT:
                        if (chapterContent == null) {
                            chapterContent = value;
                        } else {
                            this.chapterContent += value;
                        }
                        break;
                }
        } catch (SAXException e) {
            e.printStackTrace();
            Log4an.e(TAG, "解析内容错误");
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement(uri, localName, qName);
        if (localName.equals("ChapterContent")) {
            chapters.add(chapterContent);
            chapterContent = null;
        }
    }

    public List<String> getParsedData() {
        ArrayList<String> list = new ArrayList<String>();
        for (int i = 0; i < chapters.size(); i++) {
            String curChapterContent = chapters.get(i);
            // 在章节段首确认加上了两个全角空格
            try {
                if (!TextUtils.isEmpty(curChapterContent)) {
                    curChapterContent =
                            new String(Base64.decode(ROT13.rot13(curChapterContent), 0));
                    for (int j = 0; j < 10; j++) {
                        if (curChapterContent.charAt(0) == ' '
                                || curChapterContent.charAt(0) == '　') {
                            curChapterContent = curChapterContent.substring(1);
                        } else {
                            break;
                        }
                    }
                    curChapterContent = "　　" + Util.replaceByRule(curChapterContent);
                    list.add(curChapterContent);
                }
            } catch (Exception e) {
                // isNull(e);
            }
            curChapterContent = null;
        }
        chapters.clear();
        chapters = null;
        return list;
    }
}
